//
//  LocationCateoryView.m
//  DogeLegs
//
//  Created by 宋华 on 2017/9/9.
//  Copyright © 2017年 宋华. All rights reserved.
//

#import "LocationCateoryView.h"
#import "SignleCateoryCell.h"
#import "SignleCateoryView.h"

@interface LocationCateoryView () {

}

@property (strong, nonatomic)  SignleCateoryView *locationview;

@property (strong, nonatomic)  SignleCateoryView *cateoryView;

@end

@implementation LocationCateoryView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self createSubviews];
    }
    return self;
}

- (instancetype)init {
    if (self = [super init]) {
        [self createSubviews];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self createSubviews];
}

#pragma mark - 界面
- (void)createSubviews {
    WeakSelf(ws);
    [self.locationview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.mas_left).offset(0);
        make.right.mas_equalTo(ws.mas_right).offset(0);
        make.top.mas_equalTo(ws.mas_top).offset(10);
        make.width.mas_equalTo(ws.mas_width);
        make.height.mas_equalTo(40);
    }];
    
    [self.cateoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.mas_left).offset(0);
        make.right.mas_equalTo(ws.mas_right).offset(0);
        make.bottom.mas_equalTo(ws.mas_bottom).offset(-10);
        make.width.mas_equalTo(ws.mas_width);
        make.height.mas_equalTo(40);
    }];
    
    for (NSInteger i = 0; i < 2; i++) {
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = HEXCOLOR(0xf1f1f1);
        [self addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(ws.mas_left).offset(0);
            make.right.mas_equalTo(ws.mas_right).offset(0);
            if (i == 0) {
                make.top.mas_equalTo(ws.mas_top).offset(0);
            } else {
                make.bottom.mas_equalTo(ws.mas_bottom).offset(0);
            }
            make.height.mas_equalTo(10);
        }];
    }
    
    [self addTapGes];
}

- (void)addTapGes {
    UITapGestureRecognizer *ges  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseCateory:)];
    [self addGestureRecognizer:ges];
}

#pragma mark - setter

- (void)setAddress:(NSString *)address {
    if (_address != address) {
        _address = address;
        [self.locationview cellWillDisplay:PostCellType_location labelText:_address];
    }
}

- (void)setCateory:(NSString *)cateory {
    if (_cateory != cateory) {
        _cateory = cateory;
        [self.cateoryView cellWillDisplay:PostCellType_cateory labelText:_cateory];
    }
}

#pragma mark - getter
- (SignleCateoryView *)locationview {
    if (_locationview == nil) {
        _locationview = [[SignleCateoryView alloc] init];
        [self addSubview:_locationview];
      
    }
    return _locationview;
}

- (SignleCateoryView *)cateoryView {
    if (_cateoryView == nil) {
        _cateoryView = [[SignleCateoryView alloc] init];
        [self addSubview:_cateoryView];

    }
    return _cateoryView;
}

#pragma mark - 手势
- (void)chooseCateory:(UIGestureRecognizer *)ges {
//    NSLog(@"gesview is %@",ges.view);
    CGPoint point = [ges locationInView:self];
    PostCellType cellType ;
    if (CGRectContainsPoint(_locationview.frame, point)) {
        cellType = PostCellType_location;
        NSLog(@"location");
    } else if (CGRectContainsPoint(_cateoryView.frame, point)) {
        NSLog(@"cateory");
        cellType = PostCellType_cateory;
    }
    if (self.choCateoryBlock) {
        self.choCateoryBlock(cellType);
    }
}

//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    UITouch *touch = [touches anyObject];
//    if (touch.view == _locationview) {
//        NSLog(@"location");
//    } else if (touch.view == _cateoryView) {
//        NSLog(@"cateory");
//    }
//}


@end
